package com.droidnova.android.games.vortex;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.text.InputFilter;
import android.text.InputType;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class Vortex extends Activity implements ColorPickerDialog.OnColorChangedListener{
	//color variables
	private static final int DIALOG_TEXT_ENTRY = 7;
	private static final int DIALOG_FILE_ENTRY = 8;
	private static final int DIALOG_FILE_ENTRY1 = 9;  
	private String[] mFileList;
	private File mPath = new File(Environment.getExternalStorageDirectory() + "/");
	private String mChosenFile;
	private static final String FTYPE = ".txt"; 
	private static final String BRIGHTNESS_PREFERENCE_KEY = "brightness";
	private static final String COLOR_PREFERENCE_KEY = "color";
	int colorval=R.color.Blue;
	int colorval1=0;
	InputMethodManager imm;

	//static int a = 2;
	private export export1;
	private VortexRenderer _VortexRenderer;
	float _pcolors[] ,_pvertices[],_pintensity[];
	points1 pointcube1;

	int _noVertices;
	//Intent i;
	Intent myIntent;
	EditText text;

	int no;
	int r =20;

	//dynamic table variables
	TableLayout tl;
	TableRow rowtext,rowtext1;
	int _levels=0;
	TextView barray[],init_barray[];
	EditText earray[], init_earray[];
	TextView earrayzero,barrayzero;
	TableRow.LayoutParams params ,params1;
	
	//reset
	//Copy arrays for reset
	float[] copy_c,copy_v;
	int copy_noVertices;
	float copy_xmax;
	//slice
	float dec_value=0.0f;

	//initial table
	int init_no= 5;
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		//NUMPER PICKER DIALOG
		case DIALOG_TEXT_ENTRY:
			LayoutInflater factory = LayoutInflater.from(this);
			//final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry1, null);
			final View textEntryView = factory.inflate(R.layout.main1, null);
			return new AlertDialog.Builder(this)
			.setTitle("Set Number")
			.setView(textEntryView)
			.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					NumberPicker np = (NumberPicker) textEntryView.findViewById(R.id.numpic);
					_levels=np.getCurrent();
					Log.v("getvaluetext","getvaluetext->"+_levels);
					createB(_levels);
					/* User clicked OK so do some stuff */
				}
			})
			.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {

					/* User clicked Cancel so do some stuff */
				}
			})
			.create();
			//EXIT DIALOG
		case DIALOG_FILE_ENTRY:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("Are you sure you want to exit?")
			.setCancelable(false)
			.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					Vortex.this.finish();
				}
			})
			.setNegativeButton("No", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					dialog.cancel();
				}
			});
			AlertDialog alert = builder.create();
			return alert;
			//FILE PICKER DIALOG
		case DIALOG_FILE_ENTRY1:
			Dialog dialog = null;
			AlertDialog.Builder builder1 = new Builder(this);

			builder1.setTitle("Cick on the File to Select");
			if(mFileList == null){
				Log.e("log1", "Showing file picker before loading the file list");

			}
			/*builder1.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					
					
				}
			});*/
			builder1.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					dialog.cancel();
				}
			});
			
			builder1.setItems(mFileList, new DialogInterface.OnClickListener(){
				public void onClick(DialogInterface dialog, int which){
					mChosenFile = mFileList[which];
					export1.createVC(mChosenFile); 
					pointcube1.set_pcolors(export1._pcolors);
					pointcube1.set_pvertices(export1._pvertices);
					pointcube1.set_noVertices(export1._noVertices);
					_VortexRenderer.setPointcube(pointcube1);
					
					copy_c = (float[])export1._pcolors.clone();
		            copy_v =(float[])export1._pvertices.clone();
		            copy_noVertices = export1._noVertices;
		            
		            //settings for slice
		            copy_xmax = export1.getMaxXValue(copy_v);
		            dec_value=0.1f;
		            
					//Toast.makeText(Vortex.this, "File Clicked: "+ mChosenFile, Toast.LENGTH_SHORT).show();
					//you can do stuff with the file here too
				}
			});
			dialog = builder1.create();
			return dialog;

		}

		return null;
	}

	private Context context;
	int[] bcolor = new int[10];
	//ON CREATE METHOD
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		export1 = new export(this,null);
		_pcolors = export1._pcolors;
		_pvertices = export1._pvertices;
		_noVertices = export1._noVertices;
		_pintensity = export1._intensity;
		pointcube1 = new points1(export1._pcolors,export1._pvertices,export1._noVertices);
		_VortexRenderer = new VortexRenderer(this , pointcube1);
		_VortexRenderer.getHolder().setFormat(PixelFormat.RGBA_8888);

		setContentView(_VortexRenderer);
		
		
		//initial copy array for reset
		copy_c = (float[])export1._pcolors.clone();
        copy_v =(float[])export1._pvertices.clone();
        copy_noVertices = export1._noVertices;
        
        //settings for slice
        copy_xmax = export1.getMaxXValue(copy_v);
        dec_value=0.1f;
        
		
		//initial array values and colors
		final float init_values[] ={0f*(export1.max_value),.25f*(export1.max_value), .50f*(export1.max_value), .75f*(export1.max_value),1.0f*(export1.max_value)};
		//final int init_colors[] ={R.color.Yellow,R.color.Cyan,R.color.Magenta,R.color.White,R.color.Black};
		//Color color = new Color();
		//final int colors[] = {color.BLUE,color.CYAN,color.GREEN,color.RED,color.YELLOW,color.MAGENTA,color.WHITE,color.LTGRAY,Color.rgb(238,130,238),Color.rgb(244,164,96)};
		init_barray = new TextView[init_no];
		init_earray = new EditText[init_no];
		final int init_colors[]=  new int[5];
		init_colors[0]= Color.rgb(255,222,173);//Navajo White
		init_colors[1]= Color.rgb(0,0,255); //blue
		init_colors[2]= Color.rgb(0,255,0); //green
		init_colors[3]= Color.rgb(225,0,0); //red
		init_colors[4]= Color.rgb(0,0,0); //black
		//default color array
		
		bcolor[0]= Color.rgb(255,222,173);//Navajo White
		bcolor[1]= Color.rgb(0,0,255); //blue
		bcolor[2]= Color.rgb(0,255,0); //green
		bcolor[3]= Color.rgb(225,0,0); //red
		bcolor[4]= Color.rgb(255,255,0); //yellow
		bcolor[5]= Color.rgb(255,105,180); //hotpink
		bcolor[6]=Color.rgb(0,255,255); //cyan
		bcolor[7]=Color.rgb(210,105,30);//chocolate
		bcolor[8]=Color.rgb(160,32,240); //purple
		bcolor[9]=Color.rgb(255,255,255); //white

		context = this;

		//Set Table Layout
		tl = new TableLayout(this);
		tl.setStretchAllColumns(true);
		tl.setShrinkAllColumns(true);

		//Set rows
		TableRow first = new TableRow(this);
		rowtext = new TableRow(this);  
		rowtext1 = new TableRow(this);
        
		//File button
		final TextView button4 = new TextView(this);
		button4.setTextColor(getResources().getColor(R.color.Black));
		button4.setText("File");
		button4.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);  
		button4.setGravity(Gravity.CENTER);  
		button4.setTypeface(Typeface.SERIF, Typeface.BOLD);
		button4.setBackgroundResource(R.color.Gray);
		first.addView(button4);
		//OnClick listener for File button
		button4.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				loadFileList();
				showDialog(DIALOG_FILE_ENTRY1);
				//reset slice
				dec_value=0.1f;

			}
		});
		
		
		//reload button
		final TextView button1 = new TextView(this);
		button1.setClickable(false);
		button1.setTextColor(getResources().getColor(R.color.Black));
		button1.setText("Reload");	
		button1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);  
		button1.setGravity(Gravity.CENTER);  
		button1.setTypeface(Typeface.SERIF, Typeface.BOLD);
		button1.setBackgroundResource(R.color.Gray);
		first.addView(button1);
		
		//Number select button
		TextView button = new TextView(this);
		button.setTextColor(getResources().getColor(R.color.Black));
		button.setText("Levels");
		button.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);  
		button.setGravity(Gravity.CENTER);  
		button.setTypeface(Typeface.SERIF, Typeface.BOLD);
		button.setBackgroundResource(R.color.White);
		//Onclick Listener for button
		button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				showDialog(DIALOG_TEXT_ENTRY);
				button1.setClickable(true);

			}
		});
		first.addView(button);

		

		//slice button
		final TextView button3 = new TextView(this);
		button3.setTextColor(getResources().getColor(R.color.Black));
		button3.setText("Slice");
		button3.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);  
		button3.setGravity(Gravity.CENTER);  
		button3.setTypeface(Typeface.SERIF, Typeface.BOLD);
		button3.setBackgroundResource(R.color.Gray);
		first.addView(button3);
		//Onclick listener for slice
		button3.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				//recalc();
				//recalc1();
				//recalc2();
				//recalc3();
				//recalc4();
				//recalc5();
				//recalc6();
				recalc6a();
					

			}
		});

		//reset button
		final TextView button2 = new TextView(this);
		button2.setTextColor(getResources().getColor(R.color.Black));
		button2.setText("Reset");
		button2.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);  
		button2.setGravity(Gravity.CENTER);  
		button2.setTypeface(Typeface.SERIF, Typeface.BOLD);
		button2.setBackgroundResource(R.color.Gray);
		first.addView(button2);

		//On click listener for reset
		button2.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				button1.setClickable(false);
				init_createB(init_barray ,init_earray);
				init_reset();
				copy_xmax = export1.getMaxXValue(copy_v);
				dec_value=0.1f;

			}
		});

		//Table Layout params
		params1 = new TableRow.LayoutParams(); 
		params1.width=0;
		params = new TableRow.LayoutParams();

		//Create the Initial Color Table Display

		for(i=0;i<init_no;i++)
		{  
			//textviews in row2
			init_barray[i] = new TextView(this);
			//init_barray[i].setBackgroundResource(init_colors[i]);
			//init_barray[i].setBackgroundResource(bcolor[i]);
			init_barray[i].setBackgroundColor(init_colors[i]);
			//edittexts in row3
			init_earray[i] = new EditText(this);
			init_earray[i].setTextColor(getResources().getColor(R.color.White));
			init_earray[i].setBackgroundResource(R.color.Black);
			init_earray[i].setTextSize(TypedValue.COMPLEX_UNIT_DIP, 7); 
			init_earray[i].setInputType(InputType.TYPE_CLASS_NUMBER);
			init_earray[i].setFilters(new InputFilter[]{new InputFilter.LengthFilter(3)});
			init_earray[i].setGravity(Gravity.LEFT);
			init_earray[i].setText(String.valueOf(init_values[i]));
			init_earray[i].setMaxLines(1);	
			//add to row
			rowtext.addView(init_barray[i],params);	
			rowtext1.addView(init_earray[i],params);
		}


		//add Rows to TableLayout
		tl.addView(first);
		tl.addView(rowtext);
		tl.addView(rowtext1);
		tl.setGravity(Gravity.BOTTOM | Gravity.CENTER);
		this.addContentView(tl,
				new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));



		// Onclick Listener for Reload
		button1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				int f_reload =0, f_reload1=0;
				//Arrays.fill(export1._pcolors, 0);
				// button1.setBackgroundColor(Integer.parseInt(barray[2].getTag().toString()));
				for(int i=0;i<_levels;i++)
				{
					if( earray[i].getText().toString().length() == 0 || earray[i].getText().toString() == "")   
					{  
						f_reload=f_reload+1;

					}
					else if(Integer.parseInt(earray[i].getText().toString()) < 0  || Integer.parseInt(earray[i].getText().toString()) > export1.max_value || Integer.parseInt(earrayzero.getText().toString())< 0 || Integer.parseInt(earrayzero.getText().toString())>export1.max_value )

					{ 

						f_reload=f_reload+1;
					}
					else if(i<(_levels-1))
					{
						if(Integer.parseInt(earray[i].getText().toString()) > Integer.parseInt(earray[i+1].getText().toString()))
						{
							f_reload1=f_reload1+1; 
						}
					}
					else if(i==(_levels-1))
					{
						if(Integer.parseInt(earray[i].getText().toString()) > Integer.parseInt(earrayzero.getText().toString()))
						{
							f_reload1=f_reload1+1;
						}
					}
				}

				if(f_reload >0)
				{
					Toast.makeText(Vortex.this,
							"enter a value between 0 and 100 (%)",
							Toast.LENGTH_LONG).show();
				}
				else if(f_reload1 >0)
				{
					Toast.makeText(Vortex.this,
							"Please check if values are in Order",
							Toast.LENGTH_LONG).show();
				}
				else
				{
					/*for(int j=0;j<_levels;j++)
			            {

			            	Log.v("barray","barray1->"+ Float.parseFloat(barray[j].getTag().toString()));
			            	Log.v("color","color->"+earray[j].getText());

			            }*/
					/*for(int j=0;j<12;j++)
					{
						Log.v("pcolor","pcolor before->"+export1._pcolors[j]);
					}

					Log.v("before","pcolor[0]->"+ _pcolors[0]);
					Log.v("before","pcolor[1]->"+ _pcolors[1]);
					Log.v("before","pcolor[2]->"+ _pcolors[2]);
					Log.v("before","pcolor[3]->"+ _pcolors[3]);*/

					int count =0;
					int r,g,b;
					
					//export1._pcolors = null;
					for(int k=0;k<export1._noVertices;k++)
					{
						for(int m=0;m<_levels;m++)
						{
							/*if(m==(_levels-1))
							{
								if(Float.parseFloat(earray[m].getText().toString())<export1._intensity[k] && export1._intensity[k]< Float.parseFloat(earrayzero.getText().toString()))
								{
									export1._pcolors[count] = (Float.parseFloat(barray[m].getTag().toString()))%256;
									export1._pcolors[count+1] = ((Float.parseFloat(barray[m].getTag().toString()))/256)%256;
									export1._pcolors[count+2] = ((Float.parseFloat(barray[m].getTag().toString()))/256/256)%256;
									export1._pcolors[count+3] = 1.0f;
								}
							}
							else
							{

								if(Float.parseFloat(earray[m].getText().toString())<export1._intensity[k] &&  export1._intensity[k]< Float.parseFloat(earray[m+1].getText().toString()))
								{
									export1._pcolors[count] = (Float.parseFloat(barray[m].getTag().toString()))%256;
									export1._pcolors[count+1] = ((Float.parseFloat(barray[m].getTag().toString()))/256)%256;
									export1._pcolors[count+2] = ((Float.parseFloat(barray[m].getTag().toString()))/256/256)%256;
									export1._pcolors[count+3] = 1.0f;
								}

							}*/
							//int ffff = Integer.parseInt((barray[m].getTag().toString()));
							Float ffff = Float.parseFloat((barray[m].getTag().toString()));
							ffff=-1*ffff;
							//Log.v("afore -ffff","ffff->"+ ffff);
							
							//r = (int)((Math.pow(256,3)+ffff) / 65536); //where rgbs is an array of integers, every single integer represents the RGB values combined in some way
							//g = (int) (((Math.pow(256,3)+ffff) / 256 ) % 256 );
							//b = (int) ((Math.pow(256,3)+ffff)%256);

							
							/*r = Color.red(ffff);
							Log.v("color red","ffff->"+ r);
							g = Color.green(ffff);
							b = Color.blue(ffff);*/
							if(m==(_levels-1))
							{
								if(export1._intensity[k]>Float.parseFloat(earray[m].getText().toString()) && export1._intensity[k]<= Float.parseFloat(earrayzero.getText().toString()))
								{ 
									
									//Log.v("color red float","ffff->"+ (float)r/255.0f);

									//export1._pcolors[count] = (float)r/255.0f;
									//export1._pcolors[count+1]=(float)g/255.0f;
									//export1._pcolors[count+2]=(float)b/255.0f;
									//export1._pcolors[count+3] = 1.0f;
									
									export1._pcolors[count] = 1-((ffff%256)/256);
									export1._pcolors[count+1] = 1-(((ffff/256)%256)/256);
									export1._pcolors[count+2] = 1-(((ffff/(256*256))%256)/256);
									export1._pcolors[count+3] = 1.0f;
								}
							}
							else
							{

								if(export1._intensity[k]>Float.parseFloat(earray[m].getText().toString()) &&  export1._intensity[k]<= Float.parseFloat(earray[m+1].getText().toString()))
								{
									
									/*r = Color.red(ffff);
									g = Color.green(ffff);
									b = Color.blue(ffff);
									export1._pcolors[count] = (float)r/255.0f;
									export1._pcolors[count+1]=(float)g/255.0f;
									export1._pcolors[count+2]=(float)b/255.0f;
									export1._pcolors[count+3] = 1.0f;*/
									
									//export1._pcolors[count] = (float)r/255.0f;
									//export1._pcolors[count+1]=(float)g/255.0f;
									//export1._pcolors[count+2]=(float)b/255.0f;
									//export1._pcolors[count+3] = 1.0f;
									
									export1._pcolors[count] = 1-((ffff%256)/256);
									export1._pcolors[count+1] = 1-(((ffff/256)%256)/256);
									export1._pcolors[count+2] = 1-(((ffff/(256*256))%256)/256);
									export1._pcolors[count+3] = 1.0f;
								}

							}

						}


						count=count+4;                  
					}

					//Log.v("barray afore","barray[0]->"+ barray[0].getTag().toString());
					/*Log.v("afore","pcolor[0]->"+ _pcolors[0]);
					Log.v("afore","pcolor[1]->"+ _pcolors[1]);
					Log.v("afore","pcolor[2]->"+ _pcolors[2]);
					Log.v("afore","pcolor[3]->"+ _pcolors[3]);*/
					for(int j=0;j<12;j++)
					{
						Log.v("pcolor","pcolor afore->"+export1._pcolors[j]);
					}
					pointcube1.set_pcolors(export1._pcolors);
					_VortexRenderer.setPointcube(pointcube1);
				}
			}
		});
	}

	//Called to RESET color bar
	public void init_createB(TextView init_barray[], EditText init_earray[])
	{	
		rowtext.removeAllViews();
		rowtext1.removeAllViews();
		for(i=0;i<init_no;i++)
		{  			
			//add to row
			rowtext.addView(init_barray[i],params);	
			rowtext1.addView(init_earray[i],params);
		}
	}
	public void init_reset()
	{	
		pointcube1.set_pcolors(copy_c);
		pointcube1.set_pvertices(copy_v);
		pointcube1.set_noVertices(copy_noVertices);
		_VortexRenderer.setPointcube(pointcube1);
	}
	
	//Called to RESET renderer

	//Called to CUSTOMIZE COLOR
	int i;
	public void createB(int no)
	{

		int size = 7; //text size
		rowtext.removeAllViews();
		rowtext1.removeAllViews();
		barray = new TextView[no];
		earray = new EditText[no];
	
			
		for(i=0;i<no;i++)
		{  
			//textviews in row2
			barray[i] = new TextView(this);
			//barray[i].setBackgroundResource(R.color.Blue);
			//barray[i].setBackgroundResource(bcolor[i]);
			barray[i].setBackgroundColor(bcolor[i]);
			barray[i].setId(i);
			barray[i].setTag(colorval);
			//edittexts in row3
			earray[i] = new EditText(this);
			//earray[i].setId(i+10);
			earray[i].setTextColor(getResources().getColor(R.color.White));
			earray[i].setBackgroundResource(R.color.Black);
			earray[i].setTextSize(TypedValue.COMPLEX_UNIT_DIP, size); 
			earray[i].setInputType(InputType.TYPE_CLASS_NUMBER);
			//InputFilter FilterArray = new InputFilter[1];
			//FilterArray[0] = new InputFilter.LengthFilter(3);
			//earray[i].setFilters(FilterArray);
			earray[i].setFilters(new InputFilter[]{new InputFilter.LengthFilter(3)});
			earray[i].setGravity(Gravity.LEFT);
			earray[i].setText("0");
			earray[i].setMaxLines(1);
			//add to row
			rowtext.addView(barray[i],params);
			rowtext1.addView(earray[i],params);
			if(i==(no-1))
			{
				barrayzero = new TextView(this);
				barrayzero.setMaxLines(1);
				barrayzero.setBackgroundResource(R.color.Black);
				barrayzero.setTextColor(getResources().getColor(R.color.White));
				//barrayzero.setText("1");
				earrayzero = new EditText(this);
				earrayzero.setTextColor(getResources().getColor(R.color.White));
				earrayzero.setBackgroundResource(R.color.Black);
				earrayzero.setTextSize(TypedValue.COMPLEX_UNIT_DIP, size); 
				earrayzero.setInputType(InputType.TYPE_CLASS_NUMBER);
				earrayzero.setText("1");
				earrayzero.setGravity(Gravity.LEFT);
				earrayzero.setFilters(new InputFilter[]{new InputFilter.LengthFilter(3)});
				earrayzero.setMaxLines(1);
				rowtext.addView(barrayzero,params1);
				rowtext1.addView(earrayzero,params1);

			}

			barray[i].setOnClickListener(new View.OnClickListener() 
			{
				public void onClick(View cbutton) 
				{
					Log.v("clicked","clicked");
					int color = PreferenceManager.getDefaultSharedPreferences
					(Vortex.this).getInt(COLOR_PREFERENCE_KEY,Color.WHITE);
					new ColorPickerDialog(Vortex.this, Vortex.this,color).show();
					//barray[cbutton.getId()].setBackgroundColor(colorval);			
					colorval1=cbutton.getId();
				}
			});



		}


	}
	//Called when color changes
	@Override
	public void colorChanged(int color ) {
		PreferenceManager.getDefaultSharedPreferences(this).edit().putInt(COLOR_PREFERENCE_KEY, color).commit();

		colorval = color;
		barray[colorval1].setBackgroundColor(colorval);
		barray[colorval1].setTag(colorval);
	}

	//float dec_value=0.1f;
	//Called to Slice Surface
	public void recalc()
	{
       
		export1._pcolors = (float[])copy_c.clone();
		export1._pvertices = (float[])copy_v.clone();
        
		Log.v("entered recalc", "entered recalc " + "entered recalc");
		Log.v("maxvalue", "maxvalue" + dec_value);
		int i ;
		for(i =0;i<export1._pvertices.length;i+=3)
		{
			if(export1._pvertices[i]<dec_value)
			{ 

				export1._pcolors[i+4]=  0.01f;
				export1._pcolors[i+5]=  0.01f;
				export1._pcolors[i+6]=  0.02f;
			}

		}
		//max_xvalue =  max_xvalue - dec_value;
		dec_value = dec_value+0.1f;
		pointcube1.set_pcolors(export1._pcolors);

		_VortexRenderer.setPointcube(pointcube1);

	}
	
	
	//Called to Slice Surface
	public void recalc1()
	{
		
		export1._pcolors = (float[])copy_c.clone();
		export1._pvertices = (float[])copy_v.clone();
		int q=0,q1=0;  
		Log.v("entered recalc", "entered recalc " + "entered recalc");
		Log.v("maxvalue", "maxvalue" + dec_value);
		int i ;
		for(i =0;i<export1._pvertices.length;i+=3)
		{
			
			if(export1._pvertices[i]>=copy_xmax)
			{
				q =(i/3+1);
				q1=(q-1)*4;
				export1._pcolors[q1]=  0.01f;
				export1._pcolors[q1+1]=  0.01f;
				export1._pcolors[q1+2]=  0.02f;
			}
		}
	
		copy_xmax=copy_xmax-dec_value;
		Log.v("recalc","copy max->"+copy_xmax);
		pointcube1.set_pcolors(export1._pcolors);
		_VortexRenderer.setPointcube(pointcube1);

	}
	
	
	//Slice recalc2
	public void recalc2()
	{
		float[] pcolors1 = new float[export1.number];
		export1._pvertices = (float[])copy_v.clone();
		pointcube1.set_pcolors(pcolors1);
		_VortexRenderer.setPointcube(pointcube1);
		//_pcolors = null;
		float[] pcolors2 = new float[export1.number];
		int q=0,q1=0;
		int i ;
		for(i =0;i<export1._pvertices.length;i+=3)
		{
			if(export1._pvertices[i]>=copy_xmax)
			{
				q =(i/3+1);
				q1=(q-1)*4;
				for(int m=0;m<_levels;m++)
				{
					Float ffff = Float.parseFloat((barray[m].getTag().toString()));
					ffff=-1*ffff;
					if(m==(_levels-1))
					{ 
						if(export1._intensity[q]>Float.parseFloat(earray[m].getText().toString()) && export1._intensity[q]<= Float.parseFloat(earrayzero.getText().toString()))
						{
							pcolors2[q1] = 1-((ffff%256)/256);
							pcolors2[q1+1] = 1-(((ffff/256)%256)/256);
							pcolors2[q1+2] = 1-(((ffff/(256*256))%256)/256);
							pcolors2[q1+3] = 1.0f;
							
						}
					}
					else
					{
						if(export1._intensity[q]>Float.parseFloat(earray[m].getText().toString()) &&  export1._intensity[q]<= Float.parseFloat(earray[m+1].getText().toString()))
						{
							pcolors2[q1] = 1-((ffff%256)/256);
							pcolors2[q1+1] = 1-(((ffff/256)%256)/256);
							pcolors2[q1+2] = 1-(((ffff/(256*256))%256)/256);
							pcolors2[q1+3] = 1.0f;
						
						}
						
					}
				}
				
			}
		
		}
		copy_xmax=copy_xmax-dec_value;
		Log.v("recalc","copy max->"+copy_xmax);
		pointcube1.set_pcolors(pcolors2);
		_VortexRenderer.setPointcube(pointcube1);
}

		
	//Slice recalc3
	public void recalc3()
	{
		//float[] pcolors1 = new float[export1._noVertices];
		export1._pvertices = (float[])copy_v.clone();
		int q=0,q1=0;
		int i ;
		for(i =0;i<export1._pvertices.length;i+=3)
		{
			if(export1._pvertices[i]>=copy_xmax)
			{
				q =(i/3+1);
				q1=(q-1)*4;
				for(int m=0;m<_levels;m++)
				{
					//Float ffff = Float.parseFloat((barray[m].getTag().toString()));
					//ffff=-1*ffff;
					if(m==(_levels-1))
					{ 
						if(export1._intensity[q]>Float.parseFloat(earray[m].getText().toString()) && export1._intensity[q]<= Float.parseFloat(earrayzero.getText().toString()))
						{
							export1._pcolors[q1] = (Float) null;
							export1._pcolors[q1+1] =(Float) null ;
							export1._pcolors[q1+2] = (Float) null;
							export1._pcolors[q1+3] = (Float) null;
							
						}
					}
					else
					{
						if(export1._intensity[q]>Float.parseFloat(earray[m].getText().toString()) &&  export1._intensity[q]<= Float.parseFloat(earray[m+1].getText().toString()))
						{
							export1._pcolors[q1] = (Float) null;
							export1._pcolors[q1+1] = (Float) null;
							export1._pcolors[q1+2] =(Float) null;
							export1._pcolors[q1+3] = (Float) null;
						
						}
						
					}
				}
				
			}
		
		}
		copy_xmax=copy_xmax-dec_value;
		Log.v("recalc","copy max->"+copy_xmax);
		pointcube1.set_pcolors(export1._pcolors);
		_VortexRenderer.setPointcube(pointcube1);
}	
	
	
	ArrayList<Float> pcolorsList = new ArrayList<Float>();
	ArrayList <Float> pverticesList = new ArrayList <Float>();
	float[] newvertices,newpcolors;
	//Called to Slice Surface
	public void recalc4()
	{
		
		//export1._pcolors = (float[])copy_c.clone();
		//export1._pvertices = (float[])copy_v.clone();
		/*float[] pcolors1 = new float[export1.number];
		export1._pvertices = (float[])copy_v.clone();
		pointcube1.set_pcolors(pcolors1);
		_VortexRenderer.setPointcube(pointcube1);*/
		int q=0,q1=0;  
		Log.v("entered recalc", "entered recalc " + "entered recalc");
		Log.v("maxvalue", "maxvalue" + dec_value);
		int i ;
		for(i =0;i<export1._pvertices.length;i+=3)
		{
			
			if(export1._pvertices[i]<copy_xmax)
			{
				q =(i/3+1);
				q1=(q-1)*4;
				pverticesList.add(export1._pvertices[i]);
				pverticesList.add(export1._pvertices[i+1]);
				pverticesList.add(export1._pvertices[i+2]);
				pcolorsList.add(export1._pcolors[q1]);
				pcolorsList.add(export1._pcolors[q1+1]);
				pcolorsList.add(export1._pcolors[q1+2]);
				pcolorsList.add(export1._pcolors[q1+3]);
				
			}
		}
		
		export1._pcolors = toPrimitiveArray(pcolorsList);
		export1._pvertices = toPrimitiveArray(pverticesList);
		export1._noVertices = export1._pvertices.length;
		pointcube1.set_pvertices(export1._pvertices);
		pointcube1.set_pcolors(export1._pcolors);
		pointcube1.set_noVertices(export1._noVertices);
		_VortexRenderer.setPointcube(pointcube1);	
		//Float[] y =(Float[]) pverticesList.toArray();
		//float[] newvertices = toPrimitiveArray(pverticesList);
		//float[] newpcolors = toPrimitiveArray(pverticesList);
       // points1 pointcube2 = new points1(newpcolors,newvertices,newvertices.length);
		//pointcube1.set_pvertices(newvertices);
		//pointcube1.set_pcolors(newpcolors);
		//pointcube1.set_noVertices(newvertices.length);
		//_VortexRenderer.setPointcube(pointcube2);	
		
		/*Float[] array= (Float[]) pverticesList.toArray();
		float[] newvertices = ArrayUtils.toPrimitive( doubleObject );
		newvertices = new float[pverticesList.size()]; 
		newpcolors = new float[pcolorsList.size()];
		
		for (int i1 = 0; i1 < pverticesList.size(); i1++) {
		    Float f = pverticesList.get(i1);
		    newvertices[i1] = (f != null ? f : Float.NaN);
		}
		for (int i2 = 0; i2 < pcolorsList.size(); i2++) {
		    Float f = pcolorsList.get(i2);
		    newpcolors[i2] = (f != null ? f : Float.NaN);
		}
		copy_xmax=copy_xmax-dec_value;
		Log.v("recalc","copy max->"+copy_xmax);	*/

	}
	String conv_v[] , conv_c[];
	float _rv[] , _rc[];
	public void recalc5()
	{

		export1._pcolors = (float[])copy_c.clone();
		export1._pvertices = (float[])copy_v.clone();
		ArrayList <String> colors = new ArrayList<String> ();
		ArrayList <String> vertices = new ArrayList<String>();
		int q=0,q1=0;  
		for(int i =0;i<export1._pvertices.length;i+=3)
		{

			if(export1._pvertices[i]<copy_xmax)
			{
				q =(i/3+1);
				q1=(q-1)*4;
				vertices.add(String.valueOf(export1._pvertices[i]));
				vertices.add(String.valueOf(export1._pvertices[i+1]));
				vertices.add(String.valueOf(export1._pvertices[i+2]));

				colors.add(String.valueOf(export1._pcolors[q1]));
				colors.add(String.valueOf(export1._pcolors[q1+1]));
				colors.add(String.valueOf(export1._pcolors[q1+2]));
				colors.add(String.valueOf(export1._pcolors[q1+3]));


			}
		}
		conv_v = vertices.toArray(new String[vertices.size()]);
		conv_c = colors.toArray(new String[colors.size()]);

		try {
			_rv = convertStringArraytoFloatArray(conv_v);
			_rc =convertStringArraytoFloatArray(conv_c);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		copy_xmax=copy_xmax-dec_value;
		pointcube1.set_pvertices(_rv);
		pointcube1.set_pcolors(_rc);
		pointcube1.set_noVertices(_rv.length/3);
		_VortexRenderer.setPointcube(pointcube1);	
	}
	public void recalc6a()
	{

		export1._pcolors = (float[])copy_c.clone();
		export1._pvertices = (float[])copy_v.clone();
		ArrayList<Float> colors = new ArrayList<Float>();
		ArrayList <Float> vertices = new ArrayList <Float>();
		
		int q=0,q1=0;  
		for(int i =0;i<export1._pvertices.length;i+=3)
		{
			q =(i/3+1);
			if(export1._pvertices[i]<copy_xmax)
			{
				
				q1=(q-1)*4;
				
				//if(export1._intensity[i]<200)
					
				
				vertices.add(export1._pvertices[i]);
				vertices.add(export1._pvertices[i+1]);
				vertices.add(export1._pvertices[i+2]);

				colors.add(export1._pcolors[q1]);
				colors.add(export1._pcolors[q1+1]);
				colors.add(export1._pcolors[q1+2]);
				colors.add(export1._pcolors[q1+3]);


			}
			
		}
		_rv = toPrimitiveArray(vertices);
		_rc = toPrimitiveArray(colors);
		copy_xmax=copy_xmax-dec_value;
		pointcube1.set_pvertices(_rv);
		pointcube1.set_pcolors(_rc);
		pointcube1.set_noVertices(_rv.length/3);
		_VortexRenderer.setPointcube(pointcube1);	
	}
	
	public void recalc6()
	{

		export1._pcolors = (float[])copy_c.clone();
		export1._pvertices = (float[])copy_v.clone();
		ArrayList<Float> colors = new ArrayList<Float>();
		ArrayList <Float> vertices = new ArrayList <Float>();
		
		int q=0,q1=0;  
		for(int i =0;i<export1._pvertices.length;i+=3)
		{
			q =(i/3+1);
			if(export1._pvertices[i]<copy_xmax)
			{
				
				q1=(q-1)*4;
				
				//if(export1._intensity[i]<200)
					
				
				vertices.add(export1._pvertices[i]);
				vertices.add(export1._pvertices[i+1]);
				vertices.add(export1._pvertices[i+2]);

				colors.add(export1._pcolors[q1]);
				colors.add(export1._pcolors[q1+1]);
				colors.add(export1._pcolors[q1+2]);
				colors.add(export1._pcolors[q1+3]);


			}
			else if(export1._intensity[q-1]>200)
			{
								q1=(q-1)*4;
				vertices.add(export1._pvertices[i]);
				vertices.add(export1._pvertices[i+1]);
				vertices.add(export1._pvertices[i+2]);

				colors.add(export1._pcolors[q1]);
				colors.add(export1._pcolors[q1+1]);
				colors.add(export1._pcolors[q1+2]);
				colors.add(export1._pcolors[q1+3]);
			}
		}
		_rv = toPrimitiveArray(vertices);
		_rc = toPrimitiveArray(colors);
		copy_xmax=copy_xmax-dec_value;
		pointcube1.set_pvertices(_rv);
		pointcube1.set_pcolors(_rc);
		pointcube1.set_noVertices(_rv.length/3);
		_VortexRenderer.setPointcube(pointcube1);	
	}
	
	//conversion ofstring array to float array	
	public float[] convertStringArraytoFloatArray(String[] _new) throws Exception 
	{
		if (_new != null)
		{
			float floatarray[] = new float[_new.length];
			for (int i = 0; i < _new.length; i++) {
				floatarray[i] = Float.parseFloat(_new[i]);
			}
			return floatarray;
		}
		return null;
	}
	//Convertion from a object to premitive array
	private float[] toPrimitiveArray(final ArrayList<Float> vertexList) {
	    final float[] primitives = new float[vertexList.size()];
	    int index = 0;
	    for (Float object : vertexList) {
	        primitives[index++] = object;
	    }
	    return primitives;
	}
	
	//Called when loading a file
	private void loadFileList(){
		try{
			mPath.mkdirs();
		}
		catch(SecurityException e){
			Log.e("log", "unable to write on the sd card " + e.toString());
		}
		if(mPath.exists()){
			FilenameFilter filter = new FilenameFilter(){
				public boolean accept(File dir, String filename){
					File sel = new File(dir, filename);
					return filename.contains(FTYPE) || sel.isDirectory();
				}
			};
			mFileList = mPath.list(filter);
		}
		else{
			mFileList= new String[0];
		}
	}
}


